package com.zzq.springboot1.config;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
//视图解析器
@Configurable
public class mylocalResoult implements LocaleResolver {
    @Override
    //解析请求
    public Locale resolveLocale(HttpServletRequest Request) {
        //获取请求中的参数
        String langure = Request.getParameter("l");
        Locale locale = null;//如果没有使用默认的

        if(!StringUtils.isEmpty(langure)){
            //zh_cn
            String[] split = langure.split("_");
            //同一个对象没必要新建
             locale = new Locale(split[0],split[1]);

        }


        return locale ;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }

    @Bean
    public LocaleResolver myloLocaleResolver(){
        return new mylocalResoult();
    }
}
